package com.app.server.impl;

import com.app.common.SystemConstant;
import com.app.mapper.AnimalRecordMapper;
import com.app.mapper.AnimalRecordMapper;
import com.app.mapper.RecordFileMapper;
import com.app.mapper.UserMapper;
import com.app.model.AnimalRecordModel;
import com.app.model.AnimalRecordModel;
import com.app.model.RecordFileModel;
import com.app.model.UserModel;
import com.app.server.AnimalRecordService;
import com.app.server.UserService;
import com.app.utils.StringTool;
import com.app.vo.AnimalRecordVo;
import com.app.vo.AnimalRecordVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 野生动物记录
 */
@Service
public class AnimalRecordServiceImpl extends ServiceImpl<AnimalRecordMapper, AnimalRecordModel> implements AnimalRecordService {
    @Autowired
    private AnimalRecordMapper animalRecordMapper;
    @Autowired
    private RecordFileMapper recordFileMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public AnimalRecordVo getAnimalRecordList(Integer page, Integer limit, String username) {
        Page<AnimalRecordModel> page1 = new Page<>(page, limit);

        QueryWrapper<AnimalRecordModel> qw = new QueryWrapper<>();
        if (StringTool.isNotEmpty(username)) {
            qw.eq("username", username);
        }
        qw.select("id", "username", "avatar", "content", "position", "file_id", "latitude", "longitude", "open_id", "create_time", "update_time");
        qw.orderByDesc("id");
        Page<AnimalRecordModel> animalRecordPage = animalRecordMapper.selectPage(page1, qw);
        List<AnimalRecordModel> animalRecordList = animalRecordPage.getRecords();
        for (int i = 0; i < animalRecordList.size(); i++) {
            StringBuilder url = new StringBuilder();
            StringBuilder VideoUrl = new StringBuilder();
            QueryWrapper<RecordFileModel> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.orderByDesc("id");
            if (animalRecordList.get(i).getFileId() != null) {
                String[] split = animalRecordList.get(i).getFileId().split(",");
                for (int i1 = 0; i1 < split.length; i1++) {
                    RecordFileModel recordFileModel = recordFileMapper.selectById(split[i1]);
                    if (recordFileModel.getFileType() != null && recordFileModel.getFileType() == 1) {
                        url.append(recordFileModel.getUrl()).append(",");
                    } else if (recordFileModel.getFileType() != null && recordFileModel.getFileType() == 2) {
                        VideoUrl.append(recordFileModel.getUrl()).append(",");
                    }
                }
            }
            QueryWrapper<UserModel> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("open_id", animalRecordList.get(i).getOpenId());
            UserModel model = userMapper.selectOne(queryWrapper);
            if (model!=null){
                animalRecordList.get(i).setAvatar(model.getAvatar());
            }
            animalRecordList.get(i).setOpenId(null);
            animalRecordList.get(i).setUrl(url.toString());
            animalRecordList.get(i).setVideoUrl(VideoUrl.toString());
        }
        long count = animalRecordPage.getTotal();
        AnimalRecordVo animalRecordVo = new AnimalRecordVo();
        animalRecordVo.setList(animalRecordList);
        animalRecordVo.setCount((int) count);
        return animalRecordVo;
    }


    //小程序端分页查询     查询用户野生动物记录列表
    @Override
    public AnimalRecordVo getUserRecordList(Integer page, Integer limit, String openId) {

        Page<AnimalRecordModel> page1 = new Page<>(page, limit);
        QueryWrapper<AnimalRecordModel> qw = new QueryWrapper<>();
        qw.select("id", "username", "avatar", "content", "position","file_id","latitude", "longitude", "create_time", "update_time");
        qw.eq("open_id", openId);
        qw.orderByDesc("id");
        Page<AnimalRecordModel> animalRecordPage = animalRecordMapper.selectPage(page1, qw);
        List<AnimalRecordModel> animalRecordList = animalRecordPage.getRecords();
        for (int i = 0; i < animalRecordList.size(); i++) {
            StringBuilder url = new StringBuilder();
            StringBuilder VideoUrl = new StringBuilder();
            QueryWrapper<RecordFileModel> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.orderByDesc("id");
            if (animalRecordList.get(i).getFileId() != null) {
                String[] split = animalRecordList.get(i).getFileId().split(",");
                for (int i1 = 0; i1 < split.length; i1++) {
                    RecordFileModel recordFileModel = recordFileMapper.selectById(split[i1]);
                    if (recordFileModel.getFileType() != null && recordFileModel.getFileType() == 1) {
                        url.append(recordFileModel.getUrl()).append(",");
                    } else if (recordFileModel.getFileType() != null && recordFileModel.getFileType() == 2) {
                        VideoUrl.append(recordFileModel.getUrl()).append(",");
                    }
                }
            }
            animalRecordList.get(i).setUrl(url.toString());
            animalRecordList.get(i).setVideoUrl(VideoUrl.toString());
        }
        long count = animalRecordPage.getTotal();
        AnimalRecordVo animalRecordVo = new AnimalRecordVo();
        animalRecordVo.setList(animalRecordList);
        animalRecordVo.setCount((int) count);
        return animalRecordVo;
    }
}
